﻿using MusicExplorer.Model;
using MusicExplorer.Repository.Internals;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MusicExplorer.Repository
{
    [Id("AFB047FB-10A2-4E67-B167-9B6ACF3F08E1")]
    public sealed class SoundcloudRepository : RepositoryBase
    {
        private const string CLIENT_ID = "008edee592a6119fa8614bc4b2b3628c";

        public SoundcloudRepository(Model.Repository repository)
            : base(repository)
        {
            m_urlSearchEntities[EntityType.Soundcloud] =
                @"http://api.soundcloud.com/users.json?client_id={0}&limit=20&offset={1}&q={2}";
            m_urlTracks[new QueryDefinition(QueryType.AllTracks, EntityType.Soundcloud)] =
                @"http://api.soundcloud.com/users/{0}/tracks.json?client_id=008edee592a6119fa8614bc4b2b3628c&limit=20&offset={1}";
        }

        protected override string GetSearchEntitiesUrl(EntityType type, string terms, int page)
        {
            return string.Format(m_urlSearchEntities[type],
                CLIENT_ID, page * 20, terms);
        }

        protected override string GetTracksUrl(QueryType queryType, Entity entity, int page)
        {
            return string.Format(m_urlTracks[new QueryDefinition(queryType, entity.Type)],
                entity.Id, page * 20);
        }

        public override int StartingPage { get { return 0; } }
        public override bool IsMusicStore { get { return false; } }

        protected override List<Soundcloud> DecodeSoundcloud(string source)
        {
            var soundclouds = new List<Soundcloud>();
            var json_soundclouds = JArray.Parse(source);
            foreach(var soundcloudJs in json_soundclouds)
            { 
                var id = (string)soundcloudJs["id"];
                var name = (string)soundcloudJs["username"];
                var soundcloud = new Soundcloud(id, name, m_repository);
                soundclouds.Add(soundcloud);
            }
            return soundclouds;
        }

        protected override List<Model.Track> DecodeTracks(QueryType type, Entity entity, string source)
        {
            var tracks = new List<Track>();
            var json_soundclouds = JArray.Parse(source);
            foreach(var soundcloudJs in json_soundclouds)
            {
                var id = (string)soundcloudJs["id"];
                var name = (string)soundcloudJs["title"];
                var track = new Track(id, m_repository, TrackType.Soundcloud);
                track.Name = name;
                track.PublishDate = DateTime.Parse((string)soundcloudJs["created_at"]);
                track.SoundcloudStream = string.Format("{0}?client_id={1}", (string)soundcloudJs["stream_url"], CLIENT_ID);
                track.Artwork = (string)soundcloudJs["artwork_url"];
                track.Url = (string)soundcloudJs["permalink_url"];
                tracks.Add(track);
            }
            return tracks;
        }
    }
}
